package gallery.album.asset
{
    import flash.events.Event;
    import flash.events.MouseEvent;
    import flash.filters.DropShadowFilter;
    
    import gallery.asset.*;
    
    import mx.core.UIComponent;
    import mx.effects.Fade;
    import mx.effects.Move;
    import mx.effects.Parallel;
    import mx.events.EffectEvent;
    import gallery.asset.event.*;


    public class AlbumStrip extends UIComponent
    {

		private var album_arr:Array;
		private var total_lines:int = 0;
		
		//set the initial position at point 5 to give more left space
		private var x_pos:int = 5;
		private var y_pos:int = 5;

        public function AlbumStrip(){
            super();
            
        }

		override protected function createChildren():void{
		    super.createChildren();
		}

		private var _gallery:Gallery = new Gallery();
		[Bindable] public function get gallery():Gallery {
			return _gallery;
		}
		public function set gallery(value:Gallery):void {
			_gallery = value;
		}
		
		private var _thumbsize:int;
		public function get thumbsize(): int {
			return _thumbsize;
		}
		public function set thumbsize(value:int): void {
			_thumbsize = value;
		}
		
		private var _hpadding:int;
		public function get hpadding(): int {
			return _hpadding;
		}
		public function set hpadding(value:int): void {
			_hpadding = value;
		}
		
		private var _vpadding:int;
		public function get vpadding(): int {
			return _vpadding;
		}
		public function set vpadding(value:int): void {
			_vpadding = value;
		}
		
		
		private var _effectduration:int;
		public function get effectduration(): int {
			return _effectduration;
		}
		public function set effectduration(value:int): void {
			_effectduration = value;
		}
		
		
		private var _thumbnailcount:int;
		public function get thumbnailcount(): int {
			return _thumbnailcount;
		}
		public function set thumbnailcount(value:int): void {
			_thumbnailcount = value;
		}
		
		private var _imagepath:String;
		public function get imagepath(): String {
			return _imagepath;
		}
		public function set imagepath(value:String): void {
			_imagepath = value;
		}
		
		
		private var _maxlineshown:int;
		public function get maxlineshown(): int {
			return _maxlineshown;
		}
		public function set maxlineshown(value:int) : void {
			_maxlineshown = value;
		}
		
		
		
		public function init(): void {
			//every initialization, remove all children
			remove_all_children();
			album_arr = new Array();
			total_lines = 1;
			var position_ctr:int = 0;
			
			for (var i:int=0; i < gallery.photos.length; i++)
			{
			    var image:AlbumPhoto = new AlbumPhoto();
		        image.addEventListener(MouseEvent.CLICK, listener_click(gallery.photos[i].source_thumb, i));
		        image.addEventListener(MouseEvent.MOUSE_OVER, listener_hover(i));
		        image.addEventListener(MouseEvent.MOUSE_OUT, listener_out(i));
		        image.width = thumbsize;
		        image.height = thumbsize;
		        image.source = imagepath+gallery.photos[i].source_thumb;
		        image.visible = true;
		        image.buttonMode = true;
		        image.alpha = 0.5;
		        image.text = gallery.photos[i].name;
		        album_arr.push(image);
		        
		        //create the tile list layout of album
		        if(position_ctr < thumbnailcount){
		        	image.x = x_pos+(position_ctr*(thumbsize + hpadding));
		        	image.y = y_pos;
		        	position_ctr++;
		        }else{
		        	total_lines++;
		        	position_ctr = 0;
		        	x_pos = 5;
		        	y_pos = y_pos + thumbsize + hpadding + vpadding;
		        	image.x = x_pos+(position_ctr*(thumbsize + hpadding));
		        	image.y = y_pos;
		        	position_ctr++;
		        }
		        
		        addChild(image);
		        
			}
			create_mask();
		}
		
		
		
		public function navigate_up(): void {
			var parallel:Parallel = new Parallel();
			parallel.addEventListener(EffectEvent.EFFECT_END, listener_effect);
			gallery.is_enabled = false;
			
			for (var i:int=0; i < gallery.photos.length; i++)
			{
			    var move:Move = new Move();
				move.target = album_arr[i];
				move.duration = effectduration;
				move.yTo = album_arr[i].y + (thumbsize + hpadding + vpadding);
				parallel.addChild(move);
				
			}
			
			parallel.play();
		}
		
		public function navigate_down(): void {
			var parallel:Parallel = new Parallel();
			parallel.addEventListener(EffectEvent.EFFECT_END, listener_effect);
			gallery.is_enabled = false;
			
			for (var i:int=0; i < gallery.photos.length; i++)
			{ 
			    var move:Move = new Move();
				move.target = album_arr[i];
				move.duration = effectduration;
				move.yTo = album_arr[i].y - (thumbsize + hpadding + vpadding);
				parallel.addChild(move);
				
			}
			parallel.play();
		}
		
		
		public function listener_effect(evt:EffectEvent): void {
			gallery.is_enabled = true;
		}
		
		
		public function listener_click(flag:Object, index:int): Function {
			return function(): void {
				album_arr[gallery.selected].alpha = 0.5;
				album_arr[gallery.selected].filters = null;
				album_arr[index].alpha = 1;
				gallery.selected = index;
				
				dispatchEvent(new EventHandler(EventHandler.FETCH_OBJECT,{value:gallery.photos[gallery.selected].file}, true));	
			}	
		}
		
		public function listener_hover(index:int): Function {
			return function(): void {
	            var eff_fade:Fade = new Fade();
	            eff_fade.duration = effectduration;
	            eff_fade.alphaFrom = 0.5;
	            eff_fade.alphaTo = 1;
	            eff_fade.target = album_arr[index];
	            toggleDropShadow(album_arr[index]);
	            eff_fade.play();
	  		}
		}
		
		public function listener_out(index:int): Function {
			return function(): void {
	            var eff_fade:Fade = new Fade();
	            eff_fade.duration = effectduration;
	            eff_fade.alphaFrom = 1;
	            eff_fade.alphaTo = 0.5;
	            eff_fade.target = album_arr[index];
	            album_arr[index].filters = null;
	            eff_fade.play();
	  		}
		}
		
		
		public function create_mask(): void {
			var largeMask:UIComponent = new UIComponent();
			largeMask.graphics.beginFill(0x00FFFF, 0.5);
			largeMask.graphics.drawRect(0, 0, (thumbnailcount*(thumbsize+hpadding)), (thumbsize + hpadding + vpadding)*maxlineshown);
			largeMask.graphics.endFill();
			largeMask.x = 0;
			largeMask.y = 0;
			this.addChild(largeMask);
			this.mask = largeMask;
		}
		
		
		public function remove_all_children(): void {
			while (this.numChildren > 0) {     
				this.removeChildAt(0); 
			}
		}
		
		private function toggleDropShadow(image:Object):void {  
             var ds:DropShadowFilter = new DropShadowFilter(); 
             ds.distance = 1;  
             ds.alpha = .6;  
             image.filters = [ds];  
              
        }
        
        
        public function get_total_lines(): int {
        	return total_lines;
        }
        
        
   


		
    }
}